package com.youmans.userservice.controller;

import com.youmans.common_utils.config.Result;
import com.youmans.common_utils.exception.DfsException;
import com.youmans.common_utils.handler.JwtUtils;
import com.youmans.common_utils.vo.LoginVo;
import com.youmans.userservice.entity.YoumansLoginLog;
import com.youmans.userservice.entity.YoumansUser;
import com.youmans.userservice.service.YoumansLoginLogService;
import com.youmans.userservice.service.YoumansUserService;
import com.youmans.userservice.website.IpUtils;
import com.youmans.userservice.website.WebsiteApplication;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/system/user")
public class MyController {

    @Autowired
    YoumansUserService youmansUserService ;

    @PostMapping("login")
    @ResponseBody
    public Result userLogin(@RequestBody LoginVo loginVo, HttpSession
            session, HttpServletRequest request) {
        //1 获取 Subject 对象
        Subject subject = SecurityUtils.getSubject();
        Map<String,Object> map;
        //2 封装请求数据到 token 对象中
        AuthenticationToken token = new
                UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
        //3 调用 login 方法进行登录认证
        String userName = null ;
        try {
            subject.login(token);
             userName = token.getPrincipal().toString();
            session.setAttribute("user",token.getPrincipal().toString());
//            String p = token.getPrincipal().toString();
//            String pp = ((UsernamePasswordToken) token).getHost();
//            System.out.println("登录成功");
            YoumansUser user = youmansUserService.getUserInfoByName(userName);
                    if(StringUtils.isEmpty(user)){
            throw new DfsException(20001,"登录失败，账号禁用，请联系管理员") ;
        }
            String token2 = JwtUtils.getJwtToken(user.getUserId().toString(),userName) ;
            request.setAttribute("token",token2);

            map = new HashMap<>();
            map.put("token",token2);





            return Result.ok(map);
        }catch (UnknownAccountException e) {
//            e.printStackTrace();
//            System.out.println("用户名错误");
            return Result.fail().message("用户名错误");
        } catch (IncorrectCredentialsException e) {
//            e.printStackTrace();
//            System.out.println("密码错误");
            return Result.fail().message("密码错误");
        }catch (AuthenticationException e) {
//            e.printStackTrace();
//            System.out.println("密码错误");
            return Result.fail().message("用户名或密码错误");
        }
        catch (DfsException e){
//            e.printStackTrace();
//            System.out.println(e.getMessage());
            return Result.fail().message(e.getMsg());
        }
    }
}